package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.RoomBean;
import com.project.bean.StudentBean;
import com.project.mapper.IFacilityMapper;
import com.project.mapper.IRoomMapper;
import com.project.mapper.IStudentMapper;
import com.project.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoomServiceImpl implements IRoomService {

    @Autowired
    private IRoomMapper roomMapper;
    @Autowired
    private IStudentMapper studentMapper;
    @Autowired
    private IFacilityMapper facilityMapper;

    @Override
    public IPage<RoomBean> findByItem(int pageNO, String address, String state, String info, String type) {

        return roomMapper.findByItem(new Page<>(pageNO, 5), address, state, info, type);
    }

    @Override
    public void add(RoomBean room) {
        roomMapper.insert(room);

    }

    @Override
    public boolean del(Integer roomId) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("fk_roomId", roomId);
        int studentNum = studentMapper.selectCount(qw);
        if (studentNum == 0) {
            facilityMapper.delete(qw);
            roomMapper.deleteById(roomId);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public RoomBean findByRoomId(Integer id) {

        RoomBean roomBean = roomMapper.findById(id);
        roomBean.setStudent(studentMapper.findStudentByRoomId(id));
        return roomBean;
    }

    @Override
    public List<RoomBean> findByGender(String gender) {
        return roomMapper.findByGender(gender);
    }
}
